package com.qf.fmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.fmall.dao.*;
import com.qf.fmall.entity.*;
import com.qf.fmall.service.ProductService;
import com.qf.fmall.vo.PageInfoVo;
import com.qf.fmall.vo.ResStatus;
import com.qf.fmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("productService")
public class ProductServiceImpl implements ProductService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ProductMapper productMapper;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ProductImgMapper productImgMapper;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ProductParamsMapper productParamsMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ProductCommentsMapper productCommentsMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo listRecommends() {
        List<ProductVo> product = productMapper.getProduct();
        ResultVo resultVo = new ResultVo();
        if ((product.size() == 0)) {
            resultVo.setCode(ResStatus.NO);
            resultVo.setMsg("查询失败");
        } else {
            resultVo.setCode(ResStatus.OK);
            resultVo.setMsg("查询成功");
            resultVo.setData(product);
        }
        return resultVo;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo detailInfo(Integer productId) {
        ResultVo resultVo = new ResultVo();
        Product product = productMapper.selectByPrimaryKey(productId);
        Example example = new Example(ProductImg.class);
        example.createCriteria()
                .andEqualTo("itemId", productId);
        List<ProductImg> productImgs = productImgMapper.selectByExample(example);
        Example example1 = new Example(ProductSku.class);
        example1.createCriteria()
                .andEqualTo("productId", productId);
        List<ProductSku> productSkus = productSkuMapper.selectByExample(example1);
        Map<String, Object> map = new HashMap<>();
        map.put("product", product);
        map.put("productImgs", productImgs);
        map.put("productSkus", productSkus);
        resultVo.setCode(ResStatus.OK);
        resultVo.setMsg("success");
        resultVo.setData(map);
        return resultVo;
    }

    /**
     * 根据商品id查询信息
     *
     * @param productId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo detailParam(String productId) {
        ResultVo resultVo = new ResultVo();
        ProductParams productParams = new ProductParams();
        productParams.setParamId(productId);
        ProductParams params = productParamsMapper.selectOne(productParams);
        if ((params == null)) {
            resultVo.setCode(ResStatus.NO);
            resultVo.setMsg("false");
        } else {
            resultVo.setCode(ResStatus.OK);
            resultVo.setMsg("success");
            resultVo.setData(params);
        }
        return resultVo;
    }

    @Override
    public ResultVo detailCommonts(Integer productId, Integer pageNum, Integer limit) {
        ResultVo resultVo = new ResultVo();
        PageHelper.startPage(pageNum, limit);
        List<ProductCommentsVo> list = productCommentsMapper.detailCommonts(productId);
        PageInfo<ProductCommentsVo> pageInfo = new PageInfo<>(list);
        PageInfoVo<ProductCommentsVo> pageInfoVO = new PageInfoVo<>();
        pageInfoVO.setList(pageInfo.getList());
        pageInfoVO.setCount((int) pageInfo.getTotal());
        pageInfoVO.setPageCount(pageInfo.getPages());
        if (list.size() == 0) {
            resultVo.setCode(ResStatus.NO);
            resultVo.setMsg("没有查到商品的评论信息");
        } else {
            resultVo.setCode(ResStatus.OK);
            resultVo.setMsg("success");
            resultVo.setData(pageInfoVO);
        }
        return resultVo;
    }

}
